package com.lkm.market2_0.m_imple.steelyardserver.server.cmd_handle;

import com.google.gson.Gson;
import com.lkm.frame.P;
import com.lkm.frame.otto.LinkBus;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m_imple.steelyarddata.SteelyardDataModuleImple;
import com.lkm.market2_0.m_imple.steelyardserver.SteelyardServerModuleBase;
import com.lkm.market2_0.m_imple.steelyardserver.WeightChangeHelp;
import com.lkm.market2_0.m_imple.steelyardserver.server.CMDEntity;
import com.lkm.market2_0.m_imple.steelyardserver.server.ICMDHandle;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsReceiverDataHandle;
import com.lkm.market2_0.m.steelyardserver.to.WeightChangeInfo;
import com.lkm.market2_0.m.steelyardserver.event.OnWeightInfoChangeEvent;




public class CMDWeightChangeHandle implements ICMDHandle {

    SteelyardDataModule mSteelyardDataModule;
    SteelyardServerModule mSteelyardServerModule;

    public CMDWeightChangeHandle() {
        this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        this.mSteelyardServerModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
    }



    @Override
    public AbsReceiverDataHandle handle(CMDEntity cmdEntity) {
        if(this.mSteelyardDataModule==null) {
            this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        }
        if(this.mSteelyardServerModule==null) {
            this.mSteelyardServerModule =  ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
        }

        byte[] data= cmdEntity.getData();

        SteelyardInfo steelyardInfo = mSteelyardDataModule.getSteelyardInfo(cmdEntity.getSteelyardMac());

        WeightChangeInfo weightChangeInfo= WeightChangeHelp.createByData(steelyardInfo, cmdEntity.getNum(),data);
        if(weightChangeInfo==null){
            P.p("======收到的weightChangeInfo为null");
            return null;
        }
        weightChangeInfo.isUseThisDate = false;
        if(P.isDebug()&&false) {
            String logs=cmdEntity.getSteelyardMac() + new Gson().toJson(weightChangeInfo);
            P.p("======CMD称重数据" +logs );
            if(UIP.isPrint()){
                UIP.p("CMD称重："+logs,cmdEntity.getSteelyardMac());
            }
        }







        boolean isxj = false;

        if(steelyardInfo!=null) {

            {
                
                steelyardInfo.removeState(SteelyardInfo.RunTimeState_isPrint);
                steelyardInfo.removeState(SteelyardInfo.RunTimeState_salesJoin);
            }

            steelyardInfo.removeState(SteelyardInfo.RunTimeState_isAddXJ);
            isxj = steelyardInfo.hasState(SteelyardInfo.RunTimeState_subtotal);
        }




        if(!isxj)
        mSteelyardDataModule.weightChange(cmdEntity.getSteelyardMac(),weightChangeInfo);//,goodId,goodName,weightChangeInfo.getUnitPrice(),weightChangeInfo.getWeight());
        else{
            ((SteelyardDataModuleImple)mSteelyardDataModule).tryReplaceCurName(weightChangeInfo);
        }

//        SteelyardInfo steelyardInfo = mSteelyardDataModule.getSteelyardInfo(cmdEntity.getSteelyardMac());

//        steelyardInfo.removeState(SteelyardInfo.RunTimeState_subtotal);
        if(ExpandConfig.instance.isUI()){
            LinkBus.sendBroadcast(new OnWeightInfoChangeEvent(cmdEntity.getSteelyardMac(),weightChangeInfo));
        }

        mSteelyardServerModule.tickOK_block(cmdEntity.getSteelyardMac());

        return null;
    }


}
